//
//  main.swift
//  SegmentTree
//
//  Created by lhc on 2024/8/16.
//

import Foundation


func testSegmentTree() {
    
    let list = [1, 2, 3, 4, 5, -1, -2, -3, -4, -5]
    let segmentTree = SegmentTree(array: list) {
        guard let a = $0, let b = $1 else {
            return 0
        }
        
        return a + b
    }
    
    if segmentTree.query(queryLeft: 0, queryRight: list.count - 1) == 0 &&
        segmentTree.query(queryLeft: 4, queryRight: 5) == 4
    {
        print("线段树的创建和查询都正确")
    }else {
        print("线段树的创建和查询有bug")
    }
    
    segmentTree.setByIndex(5, element: 0)
    if segmentTree.query(queryLeft: 0, queryRight: list.count - 1) == 1 &&
        segmentTree.query(queryLeft: 4, queryRight: 5) == 5
    {
        print("线段树的set更新正确")
    }else {
        print("线段树的set更新有bug")
    }
}

testSegmentTree()

